package com.lei.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lei.dao.ProductDao;
import com.lei.entity.Product;

public class ProductServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String task=request.getParameter("task");
		if("all".equals(task)){
			all(request,response);
		}else if("chakan".equals(task)){
			chakan(request,response);
		}
	}

	private void chakan(HttpServletRequest request, HttpServletResponse response) {
		
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id=request.getParameter("proid");
		String name=request.getParameter("proname");
		String price=request.getParameter("proprice");
		
		Product pro=new Product();
		ProductDao pd=new ProductDao();
		pro.setId(id);
		pro.setName(name);
		pro.setPrice(2);
		pro.setShangjia(true);
	
		pd.save(pro);
		response.sendRedirect("product.jspx?task=all");
	}

	private void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session=request.getSession();
		ProductDao dao=new ProductDao();
		List<Product> list=dao.findAll();
		session.setAttribute("prolist", list);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}

}
